// pages/MyPage/My/My.js
const api = require("../../config/api.js")
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    customerCode: '',
    showDialog: false, // 控制弹框的显示与隐藏
    customerInfo: '',
    footer: {},
  },

  showLogoutDialog: function () {
    this.setData({
      showDialog: true
    });
  },

  confirmLogout() {
    wx.getStorage({
      key: 'psCustomerCode',
      success: (RES) => {
        console.log('customerCode为', RES.data)
        //退出
        wx.request({
          url: api.signOut,
          method: 'POST',
          data: {
            customerCode: RES.data
          },
          success: function (res) {
            wx.hideLoading()
            console.log(res.data)
            if (res.data.code == 200) {
              wx.showToast({
                title: "成功",
                icon: "none"
              })
            } else {
              wx.showToast({
                title: res.data.mesage,
                icon: "none"
              })
            }
          }
        })
      },
    });
    wx.removeStorage({
      key: 'psCustomerCode',
      success(res) {
        console.log(res)
      }
    })
    wx.reLaunch({
      url: '/pages/Login/Login',
    })
  },

  cancelLogout() {
    this.setData({
      showDialog: false,
    });
  },

  onMyInfo: function (e) {
    wx.navigateTo({
      url: '/pages/MyPage/MyInfo/MyInfo',
    })
  },

  onMyEstimations: function (e) {
    wx.navigateTo({
      url: '/pages/MyPage/MyEstimations/MyEstimations',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getfooter()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.popup = this.selectComponent("#popup");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.getStorage({
      key: 'psCustomerCode',
      success: (res) => {
        this.setData({
          customerCode: res.data
        });

        wx.request({
          url: api.customerInfo,
          method: 'POST',
          data: {
            customerCode: this.data.customerCode, // 使用 data 中的值
          },
          success: (res) => {
            wx.hideLoading()
            if (res.data.code == 200) {
              console.log("我的信息：")
              console.log(res.data.data)
              this.setData({
                customerInfo: res.data.data
              })
            } else {
              wx.showToast({
                title: res.data.mesage,
                icon: "none"
              })
            }
          }
        })
      },
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 初始化底部Tab数据
   */

  getfooter: function () {
    var that = this
    wx.getStorage({
        key: 'psCustomerCode',
        success: (RES) => {
            that.setData({
                customerCode: RES.data
            });
            console.log('customerCode为', RES.data)
            wx.request({
                url: api.findUnread,
                method: 'POST',
                data: {
                    psCustomerCode: that.data.customerCode,
                },
                success: function (res) {
                    if (res.data.code == 200) {
                        that.setData({
                            unreadNum: res.data.data.unreadNum,
                        }),
                        console.log("未读消息数量:", that.data.unreadNum)
                        // var t = this;
                        var footerList = {
                            footerTab: 4,
                            footerlist: [{
                                diy_name: "设备概览",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/index/index"
                            }, {
                                diy_name: "实时监测",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexMonitor/indexMonitor",
                                unreadNum: that.data.unreadNum
                            }, {
                                diy_name: "",
                                color: '#221915',
                                img: "https://www.cqwuyang.cn/images/Recognition.png",
                                selimg: "https://www.cqwuyang.cn/images/Recognition.png",
                                link: ""
                            }, {
                                diy_name: "上报总览",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexSummary/indexSummary"
                            }, {
                                diy_name: "个人中心",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexMy/indexMy"
                            }]
                        }
                        that.setData({
                            footer: footerList
                        })
                    }

                }
            })
        },
    });



},

  /**
   * 底部上报弹窗
   */
  changeRange: function () {
    this.popup.changeRange()
  },

  /**
   * 退出弹窗
   */
  outPop: function () {
    wx.showModal({
      title: '提示',
      content: '是否退出登录？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.clearStorage()
          wx.showToast({
            title: "退出成功",
            icon: "none"
          })
          wx.redirectTo({
            url: "/pages/Login/Login"
          });
        } else {
          console.log('用户点击取消')
        }
      }
    })
  }
})